﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace project_web2.AdminTool {
    public partial class AddandEditCategories : System.Web.UI.Page {
        DAL dal = new DAL();
        protected void Page_Load(object sender, EventArgs e) {
          if(!IsPostBack){
              if (Request.QueryString["id"] != null) {
                  int id = Convert.ToInt32(Request.QueryString["id"].ToString());
                  bindData(id);
              } else {
                    bindListBox(27, getAllData());
              }
              
          }
        }
        private void bindData(int id) {
            DataTable dt = getAllData();
            if(dt == null){ return; }
            
            DataRow dr = getRowById(id, dt);
            txtHide.Value = id.ToString();
            txtName.Text = dr["categoryname"].ToString();
            txtDescription.Text = dr["description"].ToString();
            bindListBox(id, dt);
            string status = dr["status"].ToString();
            if(status == "True"){
                chbStatus.Checked = true;
            } else{
                chbStatus.Checked = false;
            }
        }

        private void bindListBox(int id, DataTable dt) {
            lbCategories.DataSource = dt;
            lbCategories.DataTextField = "categoryname";
            lbCategories.DataValueField = "categoryid";
            lbCategories.DataBind();
            lbCategories.SelectedValue = id.ToString();
        }
        private DataTable getAllData() {
            string sql = "select categoryid, categoryname, description, parentid, status from Categories";
            DataTable dt = dal.get_data(sql, false);
            return dt;
        }
        private DataRow getRowById(int id, DataTable dt){
            DataRow dr = null;
            foreach (DataRow r in dt.Rows) {
                if (r["categoryid"].ToString() == id.ToString()) {
                    dr = r;
                    break;
                }
            }
            return dr;
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            string id = txtHide.Value;
            string name = txtName.Text;
            string des = txtDescription.Text;
            string paId = lbCategories.SelectedValue;
            bool sta = false;
            if (chbStatus.Checked) {
                sta = true;
            }
            if (Request.QueryString["id"] != null) {
                updateData(id, name, des, paId, sta);
            } else {
                insertData(name, des, paId, sta);
            }
            
        }
        private void updateData(string id, string name, string des, string paid, bool sta) {
            string sql = "update Categories set categoryname = N'" + name + "' ,description = N'" + des +
                "' ,parentid = '" + paid + "' ,status='" + sta + "' where categoryid='" + id + "'";
            dal.executeQuery(sql);
        }
        private void insertData(string name, string des, string paid, bool sta) {
            string sql = "insert into  Categories values(N'" + name  +
            "', GETDATE(), GETDATE(),'" + des + "','" + paid + "','" + sta + "')";
            dal.executeQuery(sql);
        }
        protected void btnExit_Click(object sender, EventArgs e) {
            Response.Redirect("ManagerCategories.aspx");
        }
    }
}